package one.bugu.android.demon.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import android.widget.TextView;
import com.juefeng.android.framework.common.util.ToastUtils;
import com.juefeng.android.framework.update.AppVersionUpdateService;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.HashMap;
import java.util.Map;

import one.bugu.android.demon.BuildConfig;
import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.CheckUpdateBean;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.StatusBarUtils;
import one.bugu.android.demon.util.VersionUtils;

/**
 * 关于我们界面
 */
@LKContentView(R.layout.activity_aboutus)
public class AboutUsActivity extends MyBaseActivity{

    /**
     * topBar
     */
    @LKInjectView(R.id.btbv_about_us)
    private BaseTopBarView baseTopBarView;

    @LKInjectView(R.id.version_textview)
    private TextView versionText;

    /**
     * 更新按钮
     */
    @LKInjectView(R.id.btn_update)
    private Button btnUpdate;

    @Override
    protected void initView() {
        super.initView();
        //设置状态栏
        StatusBarUtils.getInstance().setStatusBar("#5856D0",this, baseTopBarView, true);
    }

    @Override
    protected void initData() {
        super.initData();
        versionText.setText(VersionUtils.getVerName(this));
    }


    @Override
    protected void initEvent() {
        super.initEvent();

        //返回点击事件
       baseTopBarView.setEventInterface(new BaseTopBarView.EventInterface() {
           @Override
           public void leftOnClick() {
               finish();
           }

           @Override
           public void leftCloseClick() {

           }

           @Override
           public void rightOnClick() {

           }
       });

        //更新点击事件
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String verName = VersionUtils.getVerName(AboutUsActivity.this);
                if (verName==null){
                    verName = "";
                }
                Map<String, String> map = new HashMap<>();
                map.put("appId", HttpConstant.APPID);
                map.put("verCode", verName.replace(".",""));
                map.put("channel", BuildConfig.FLAVOR);
                AppVersionUpdateService appVersionUpdateService = new AppVersionUpdateService(AboutUsActivity.this, HttpConstant.CHECK_DEAL, map, CheckUpdateBean.class, new AppVersionUpdateService.OnUpdateListener() {
                    @Override
                    public void success(Object o) {
                        Intent intent= new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        Uri content_url = Uri.parse(((CheckUpdateBean)o).getUrl());
                        intent.setData(content_url);
                        startActivity(intent);
                    }

                    @Override
                    public void nothing() {
                        ToastUtils.custom("已经是最新版本了");
                    }
                });
                appVersionUpdateService.update();

            }
        });
    }
}
